package com.xueyi.system.BaseMyMsg.controller;

import com.xueyi.system.BaseMyMsg.domain.query.BaseMyMsgQuery;
import com.xueyi.system.BaseMyMsg.domain.dto.BaseMyMsgDto;
import com.xueyi.system.BaseMyMsg.service.IBaseMyMsgService;
import com.xueyi.common.web.entity.controller.BaseController;
import com.xueyi.system.api.dict.domain.dto.SysDictDataDto;
import com.xueyi.system.api.dict.domain.query.SysDictDataQuery;
import com.xueyi.system.dict.service.ISysDictDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.xueyi.common.security.annotation.Logical;
import com.xueyi.common.security.annotation.RequiresPermissions;
import com.xueyi.common.core.web.validate.V_A;
import com.xueyi.common.core.web.validate.V_E;
import com.xueyi.common.core.web.result.AjaxResult;
import com.xueyi.common.log.annotation.Log;
import com.xueyi.common.log.enums.BusinessType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletResponse;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 我的消息管理 业务处理
 *
 * @author xueyi
 */
@RestController
@RequestMapping("/BaseMyMsg")
public class BaseMyMsgController extends BaseController<BaseMyMsgQuery, BaseMyMsgDto, IBaseMyMsgService> {


    @Autowired
    private IBaseMyMsgService baseMyMsgService;

    @Autowired
    private ISysDictDataService dictDataService;

    /** 定义节点名称 */
    @Override
    protected String getNodeName() {
        return "我的消息" ;
    }

    /**
     * 查询我的消息列表
     */
    @Override
    @GetMapping("/list")
    @RequiresPermissions(Auth.BASE_MY_MSG_LIST)
    public AjaxResult list(BaseMyMsgQuery baseMyMsg) {
        return super.list(baseMyMsg);
    }

    /**
     * 查询我的消息详细
     */
    @Override
    @GetMapping(value = "/{id}")
    @RequiresPermissions(Auth.BASE_MY_MSG_SINGLE)
    public AjaxResult getInfoExtra(@PathVariable Serializable id) {
        return super.getInfoExtra(id);
    }

    /**
     * 我的消息导出
     */
    @Override
    @PostMapping("/export")
    @RequiresPermissions(Auth.BASE_MY_MSG_EXPORT)
    @Log(title = "我的消息管理", businessType = BusinessType.EXPORT)
    public void export(HttpServletResponse response, BaseMyMsgQuery baseMyMsg) {
        super.export(response, baseMyMsg);
    }

    /**
     * 我的消息新增
     */
    @Override
    @PostMapping
    @RequiresPermissions(Auth.BASE_MY_MSG_ADD)
    @Log(title = "我的消息管理", businessType = BusinessType.INSERT)
    public AjaxResult add(@Validated({V_A.class}) @RequestBody BaseMyMsgDto baseMyMsg) {
        return super.add(baseMyMsg);
    }

    /**
     * 我的消息修改
     */
    @Override
    @PutMapping
    @RequiresPermissions(Auth.BASE_MY_MSG_EDIT)
    @Log(title = "我的消息管理", businessType = BusinessType.UPDATE)
    public AjaxResult edit(@Validated({V_E.class}) @RequestBody BaseMyMsgDto baseMyMsg) {
        return super.edit(baseMyMsg);
    }

    /**
     * 我的消息修改状态
     */
    @Override
    @PutMapping("/status")
    @RequiresPermissions(value = {Auth.BASE_MY_MSG_EDIT, Auth.BASE_MY_MSG_ES}, logical = Logical.OR)
    @Log(title = "我的消息管理", businessType = BusinessType.UPDATE_STATUS)
    public AjaxResult editStatus(@RequestBody BaseMyMsgDto baseMyMsg) {
        return super.editStatus(baseMyMsg);
    }

    /**
     * 我的消息批量删除
     */
    @Override
    @DeleteMapping("/batch/{idList}")
    @RequiresPermissions(Auth.BASE_MY_MSG_DEL)
    @Log(title = "我的消息管理", businessType = BusinessType.DELETE)
    public AjaxResult batchRemove(@PathVariable List<Long> idList) {
        return super.batchRemove(idList);
    }


    @GetMapping("/getMsgType")
    public AjaxResult getMsgType(BaseMyMsgQuery baseMyMsg) {

        SysDictDataQuery dataQuery = new SysDictDataQuery();
        dataQuery.setCode("base_msg_type");
        List<SysDictDataDto> dtoList = dictDataService.selectList(dataQuery);

        List list = new ArrayList<>();
        Map map = new HashMap<>();
        for (SysDictDataDto dictDataDto : dtoList) {
            baseMyMsg.setType(dictDataDto.getValue());
            List<BaseMyMsgDto> msgDtoList = baseMyMsgService.selectList(baseMyMsg);
            map.put(dictDataDto.getValue(),msgDtoList);
            list.add(map);
        }
        return AjaxResult.success(map);
    }



    /**
     * 获取我的消息选择框列表
     */
    @Override
    @GetMapping("/option")
    public AjaxResult option() {
        return super.option();
    }

    interface Auth {
        /** 系统 - 我的消息管理 - 列表 */
        String BASE_MY_MSG_LIST = "system:BaseMyMsg:list" ;
        /** 系统 - 我的消息管理 - 详情 */
        String BASE_MY_MSG_SINGLE = "system:BaseMyMsg:single" ;
        /** 系统 - 我的消息管理 - 新增 */
        String BASE_MY_MSG_ADD = "system:BaseMyMsg:add" ;
        /** 系统 - 我的消息管理 - 修改 */
        String BASE_MY_MSG_EDIT = "system:BaseMyMsg:edit" ;
        /** 系统 - 我的消息管理 - 修改状态 */
        String BASE_MY_MSG_ES = "system:BaseMyMsg:es" ;
        /** 系统 - 我的消息管理 - 删除 */
        String BASE_MY_MSG_DEL = "system:BaseMyMsg:delete" ;
        /** 系统 - 我的消息管理 - 导出 */
        String BASE_MY_MSG_EXPORT = "system:BaseMyMsg:export" ;
    }
}
